﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DevComponents.DotNetBar.Controls;
using System.Data;

namespace BUL
{
    public partial class Business
    {      
        public void GetAllNSX(string strTimkiem, DataGridViewX dataGridViewX )
        {
            if(dataGridViewX.RowCount>0)
                dataGridViewX.Rows.Clear();
            using (var dal = new QuanLyBanHangEntities1())
            {
                var listResult = dal.tblNhaSanXuats.Where(x => x.TenNSX.Contains(strTimkiem)).OrderBy(x => x.TenNSX);
                int i = 0;
                foreach (var item in listResult)
                {
                    dataGridViewX.Rows.Add();
                    dataGridViewX.Rows[i].Cells[0].Value = "N";
                    dataGridViewX.Rows[i].Cells[1].Value = item.MaNSX;
                    dataGridViewX.Rows[i].Cells[2].Value = item.TenNSX;
                    dataGridViewX.Rows[i].Cells[3].Value = item.GhiChu;
                    i++;
                }
            }
        }

        public tblNhaSanXuat GetNSXById(string mansx)
        {
            return _dal.tblNhaSanXuats.Where(x => x.MaNSX == mansx).FirstOrDefault();
        }

        public void InsertNSX(tblNhaSanXuat nsx)
        {
            _dal.tblNhaSanXuats.AddObject(nsx);
            _dal.SaveChanges();
        }

        public void UpdateNSX(string maNSXCu, tblNhaSanXuat nsx)
        {
            var NSXUpdate = _dal.tblNhaSanXuats.Where(x => x.MaNSX == maNSXCu).FirstOrDefault();
            NSXUpdate.MaNSX = nsx.MaNSX;
            NSXUpdate.TenNSX = nsx.TenNSX;
            NSXUpdate.GhiChu = nsx.GhiChu;
            _dal.SaveChanges();
        }

        public void DeleteNSX(IEnumerable<string> mansxs)
        {
            var listDelete = _dal.tblNhaSanXuats.Where(x => mansxs.Contains(x.MaNSX));
            foreach (var tblnsx in listDelete)
            {
                _dal.tblNhaSanXuats.DeleteObject(tblnsx);
            }
            _dal.SaveChanges();
        }

        public tblNhaSanXuat BuildNSX(string maNSX, string tenNSX, string ghichu)
        {
            return new tblNhaSanXuat { MaNSX = maNSX, TenNSX = tenNSX,GhiChu = ghichu };
        }
    }
}
